/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.ocr.jocrtools;

import gr.anagnosg.general.utils.ExtrernalUtils;
import gr.anagnosg.general.utils.FileUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author anagnosg
 */
public class TesseractUtils {
    
    
    //tesseract ell.verdana.exp0.tif ell.verdana.exp0 -l ell batch.nochop makebox ;
    public static String createBoxFile(String filename){
        /*String pathPrefix=ft.getParent()+File.separatorChar;
            String fn =FileUtils.getFilenameNoExt(ft);
            String commandConvertPmg = " convert "+pathPrefix+ft.getName()+" "+pathPrefix+fn+"."+toExt;
            System.out.println(commandConvertPmg);
            String output = ExtrernalUtils.executeCommand(commandConvertPmg);
            File fnT = new File(ft.getParent()+File.separatorChar+fn+"."+toExt);
        */
        File ft = new File(filename);
        String pathPrefix=ft.getParent()+File.separatorChar;
        String fn =FileUtils.getFilenameNoExt(ft);
        String command = " tesseract "+filename+" "+pathPrefix+fn+" -l ell batch.nochop makebox";
        System.out.println("Command"+command);
        String output = ExtrernalUtils.executeCommand(command);
        String outputFilename = pathPrefix+fn+".box";
        return outputFilename;
        
    }
    
    public static List readBoxFile(String filename){
        List output =new ArrayList();
        
        String s1 = FileUtils.readTextFile(filename);
        String[] s1Array= s1.split("\n");
        for(int i = 0 ; i < s1Array.length ; i++){
            String st = s1Array[i];
            String[] lineArray = st.split(" ");
            HashMap hm = new HashMap();
            /*hm.put("n0", lineArray[0]);
            hm.put("n1", lineArray[1]);
            hm.put("n2", lineArray[2]);
            hm.put("n3", lineArray[3]);
            hm.put("n4", lineArray[4]);
            hm.put("n5", lineArray[5]);
            */
            output.add(lineArray);
        }
        return output;
    
    }
    
    public static void saveBoxFile(String filename,List data) {
        File f = new File(filename);
        BufferedWriter fout =null;
        try{
            f.createNewFile();
            fout = new BufferedWriter(new FileWriter(f));
            for(int i = 0 ;i < data.size(); i++){
                String[] line = (String[])data.get(i);
                //writer = new BufferedWriter(new FileWriter(logFile));
                fout.write(line[0]+" "+line[1]+" "+line[2]+" "+line[3]+" "+line[4]+" "+line[5]+System.getProperty("line.separator"));
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        finally{
            try{
                if(fout!=null){
                    fout.close();
                }
            }
            catch(Exception ex2){ex2.printStackTrace();}
        }
    }
    
    //tesseract ell.verdana.exp0.tif ell.verdana.exp0 box.train; 
    public static String createTrainData(String filename){
        File ft = new File(filename);
        String pathPrefix=ft.getParent()+File.separatorChar;
        String fn =FileUtils.getFilenameNoExt(ft);
        String command = " tesseract "+filename+" "+pathPrefix+fn+" -l ell box.train";
        System.out.println("Command"+command);
        String output = ExtrernalUtils.executeCommand(command);
        String outputFilename = pathPrefix+fn+".tr";
        return outputFilename;
    }
    
    /*public static List createTrainData(File[] files){
        
    
    }*/
    
    public static void extractUnichar(String[] boxfiles){
     //unicharset_extractor ell.verdana.exp0.box ell.verdana.exp1.box ell.verdana.exp2.box ell.verdana.exp3.box ell.verdana.exp4.box ell.verdana.exp5.box ell.verdana.exp6.box ell.verdana.exp7.box ell.verdana.exp8.box ell.verdana.exp9.box ell.verdana.exp10.box ell.verdana.exp11.box ell.verdana.exp12.box;   
     String command = "unicharset_extractor ";
     for(int i = 0 ; i < boxfiles.length;i++){
            command +=" "+boxfiles[i];
     
     }
     String output = ExtrernalUtils.executeCommand(command);
     
    }
    
    
    public static void clustering(String[] files, String fontPropertiesFile,String unicharsetFile){
        //shapeclustering -F font_properties -U unicharset lang.fontname.exp0.tr
     String command1 = "shapeclustering  -F" +fontPropertiesFile +" -U "+unicharsetFile +" ";
     //mftraining -F font_properties -U unicharset -O lang.unicharset lang.fontname.exp0.tr lang.fontname.exp1.tr ...
     String command2 = "mftraining -F "+fontPropertiesFile + " -U "+unicharsetFile +" ";
     //cntraining lang.fontname.exp0.tr lang.fontname.exp1.tr ...
     String command3 = "cntraining  ";
     for(int i = 0 ; i < files.length;i++){
            command1 +=" "+files[i];
            command2 +=" "+files[i];
            command3 +=" "+files[i];
     }
     String output = ExtrernalUtils.executeCommand(command1);
     String output2 = ExtrernalUtils.executeCommand(command2);
     String output3 = ExtrernalUtils.executeCommand(command3);
     
     
     
    }
    
    
    public static HashMap renameConfigFiles(String pathPrefix,String langPrefix){
    /*mv inttemp ell.inttemp;
    mv normproto ell.normproto;
mv pffmtable ell.pffmtable;
mv shapetable ell.shapetable;
        
        */
        HashMap hm =new HashMap();
        String command="mv "+pathPrefix+"inttemp "+pathPrefix+langPrefix+".inttemp";
        String command2="mv "+pathPrefix+"normproto "+pathPrefix+langPrefix+".normproto";
        String command3="mv "+pathPrefix+"pffmtable "+pathPrefix+langPrefix+".pffmtable";
        String command4="mv "+pathPrefix+"shapetable "+pathPrefix+langPrefix+".shapetable";
        String command5="mv "+pathPrefix+"unicharset "+pathPrefix+langPrefix+".unicharset";
        
        String output = ExtrernalUtils.executeCommand(command);
        String output2 = ExtrernalUtils.executeCommand(command2);
        String output3 = ExtrernalUtils.executeCommand(command3);
        String output4 = ExtrernalUtils.executeCommand(command4);
        
        
        return hm;
    }
    
    
    //wordlist2dawg frequent_words_list lang.freq-dawg lang.unicharset
    //wordlist2dawg words_list lang.word-dawg lang.unicharset
    
    public static void dataDictionary(String filename,int dictionaryType, String lang, String unicharsetFile){
        String dictionaryFile = "";
        String parent  = new File(filename).getParent()+File.separatorChar;
        String command="wordlist2dawg "+filename+" "+parent+lang+"."+dictionaryFile;
    }
        
}
